<?php
    class Employe extends Personne {
        //Alias sur table : e

        //Constructeur complet
        //(Civilite)civilite, (Profil)profil
        public function __construct($id_employe, $civilite, $profil, $mdp, $nom, $prenom, $telephone, $mobile, $email, $date_naissance, 
                                    $date_ajout, $date_modif, $valide) {
            $this->setId($id_employe);
            $this->setCivilite($civilite);
            $this->setProfil($profil);
            $this->setMdp($mdp);
            $this->setPrenom($prenom);
            $this->setNom($nom);
            $this->setTelephone($telephone);
            $this->setMobile($mobile);
            $this->setEmail($email);
            $this->setDateNaissance($date_naissance);
            $this->setDateAjout($date_ajout);
            $this->setDateModif($date_modif);
            $this->setValide($valide);
        }
        
        //Affiche le nom de l'Employe
        //Paramètre : aucun
        //Retourne (String)
        public function __toString() {
            return $this->prenom . ' ' . $this->nom;
        }
        
        //Ajoute un Employe à la BDD
        //Paramètre : aucun
        //Retourne (Employe) 
        public function add() {
            $sql =  'INSERT INTO employe (id_civilite, id_profil, mdp, prenom, nom, telephone, mobile, email, date_naissance, date_ajout, 
                                        date_modif, valide) VALUES
                    ('.intval($this->civilite->getId()).', '.intval($this->profil->getId()).', "'.Db::insertion($this->mdp).'", 
                    '.Db::insertion($this->prenom).', "'.Db::insertion($this->nom).'", "'.Db::insertion($this->telephone).'", 
                    "'.Db::insertion($this->mobile).'", "'.Db::insertion($this->email).'", "'.$this->getDateNaissance().'", 
                    "'.$this->getDateAjout().'", "'.$this->getDateModif().'", '.intval($this->valide).')';
            $this->setId(Db::Execute($sql));
            return $this;
        }
        
        //Modifie un Employe dans la BDD
        //Paramètre : aucun
        //Retourne (Employe)
        public function update() {
            $this->setDateModif(new DateTime());
            $sql =  'UPDATE employe
                    SET id_civilite = '.intval($this->civilite->getId()).',
                        id_profil = '.intval($this->profil->getId()).', 
                        prenom = "'.Db::insertion($this->prenom).'", 
                        nom = "'.Db::insertion($this->nom).'", 
                        telephone = "'.Db::insertion($this->telephone).'",
                        mobile = "'.Db::insertion($this->mobile).'", 
                        email = "'.Db::insertion($this->email).'",
                        date_naissance = "'.$this->getDateNaissance().'",
                        date_modif = "'.$this->getDateModif().'",
                        valide = '.intval($this->valide).'
                    WHERE
                        id_employe = '.intval($this->getId());
            Db::Execute($sql);
            return $this;
        }
        
        //Permet de désactiver un Employe
        //Paramètre : aucun
        //Retourne (Employe) 
        public function suppr() {
            $this->setValide(false);
            $this->update();
        }
        
        //Regarde si l'utilisateur à accès au module passé en paramètre
        //Paramètre : (Module)module
        //Retourne (Access)
        public function autorisation($module) {
            if($this->valide == 1 && $module->getValide() == 1) {
                $sql =  'SELECT
                            a.droit as adroit
                        FROM acces a
                        WHERE
                            a.id_profil = '.intval($this->profil->getId()).'
                        AND a.id_module = '.intval($module->getId());
                $ps = Db::Execute($sql, true);
                foreach($ps as $p) {
                    return new Access($module, $this->profil, $p['adroit']);
                }
            }
            else {
                return new Access($module, $this->profil, 0);
            }
            
            return null;
        }
        
        //Permet la modification de mot de passe
        //Paramètre : (String)ancien_mdp, (String)new_mdp, (String)confirmation
        //Retourne (Booléen)
        public function changementMdp($ancien_mdp, $new_mdp, $confirmation) {
            //On regarde si les deux mots de passe sont égaux
            if($confirmation !== $new_mdp) {
                return false;
            }
            
            $temp = "";
            //On récupère l'ancien mot de passe de cet Employe
            $sql =  'SELECT
                        e.mdp as emdp
                    FROM employe e
                    WHERE
                        e.id_employe = '.intval($this->getId());
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $temp = $p['emdp'];
            }
            
            //On fait la confirmation pour l'ancien mot de passe
            if($ancien_mdp !== $temp) {
                return false;
            }
            
            $this->setMdp(Tools::encrypt($new_mdp));
            $this->update();
            return true;
        }
        
        //Récupère tout les employés enregistrés
        //Paramètre : aucun
        //Retourne une collection de Employe
        public static function getEmployes() {
            $sql =  'SELECT
                        e.id_employe as eid_employe, e.mdp as emdp, e.prenom as eprenom, e.nom as enom, e.telephone as etelephone, e.mobile as emobile, 
                        e.email as eemail, e.date_naissance as edate_naissance, e.date_ajout as edate_ajout, e.date_modif as edate_modif, e.valide as evalide,  
                        pro.id_profil as proid_profil, pro.libelle as prolibelle, pro.valide as provalide,
                        civ.id_civilite as civid_civilite, civ.libelle as civlibelle, civ.diminutif as civdiminutif, civ.valide as civvalide
                    FROM employe e
                    LEFT JOIN profil pro ON e.id_profil = pro.id_profil
                    LEFT JOIN civilite civ ON e.id_civilite = civ.id_civilite';
            $tabRetour = array();
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
               $profil = new Profil($p['proid_profil'], $p['prolibelle'], $p['provalide']);
               $civilite = new Civilite($p['civid_civilite'], $p['civlibelle'], $p['civdiminutif'], $p['civvalide']);
               $tabRetour[] = new Employe($p['eid_employe'], $civilite, $profil, $p['emdp'], $p['enom'], $p['eprenom'], 
                                   $p['etelephone'], $p['emobile'], $p['eemail'], $p['edate_naissance'], $p['edate_ajout'], 
                                   $p['edate_modif'], $p['evalide']);
            }
            return $tabRetour;
        }
        
        //Regarde si le couple email/mdp est juste
        //Paramètre : (String)email, (String)mdp
        //Retourne (Employe)
        public static function connect($email, $mdp) {
            $sql =  'SELECT
                        e.id_employe as eid_employe, e.mdp as emdp, e.prenom as eprenom, e.nom as enom, e.telephone as etelephone, e.mobile as emobile, 
                        e.email as eemail, e.date_naissance as edate_naissance, e.date_ajout as edate_ajout, e.date_modif as edate_modif, e.valide as evalide,  
                        pro.id_profil as proid_profil, pro.libelle as prolibelle, pro.valide as provalide,
                        civ.id_civilite as civid_civilite, civ.libelle as civlibelle, civ.diminutif as civdiminutif, civ.valide as civvalide
                    FROM employe e
                    LEFT JOIN profil pro ON e.id_profil = pro.id_profil
                    LEFT JOIN civilite civ ON e.id_civilite = civ.id_civilite
                    WHERE
                        e.email = "'.Db::insertion($email).'"
                    AND e.mdp = "'.Db::insertion(Tools::encrypt($mdp)).'"';
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $profil = new Profil($p['proid_profil'], $p['prolibelle'], $p['provalide']);
                $civilite = new Civilite($p['civid_civilite'], $p['civlibelle'], $p['civdiminutif'], $p['civvalide']);
                return new Employe($p['eid_employe'], $civilite, $profil, $p['emdp'], $p['enom'], $p['eprenom'], 
                                    $p['etelephone'], $p['emobile'], $p['eemail'], $p['edate_naissance'], $p['edate_ajout'], 
                                    $p['edate_modif'], $p['evalide']);
            }
            
            return null;
        }
         
        //Crée un objet Employe grâce à son ID
        //Paramètre : (int)id
        //Retourne (Employe)
        public static function rechercheEmploye($id) {
            $sql =  'SELECT
                        e.id_employe as eid_employe, e.mdp as emdp, e.prenom as eprenom, e.nom as enom, e.telephone as etelephone, e.mobile as emobile, 
                        e.email as eemail, e.date_naissance as edate_naissance, e.date_ajout as edate_ajout, e.date_modif as edate_modif, e.valide as evalide,  
                        pro.id_profil as proid_profil, pro.libelle as prolibelle, pro.valide as provalide,
                        civ.id_civilite as civid_civilite, civ.libelle as civlibelle, civ.diminutif as civdiminutif, civ.valide as civvalide
                    FROM employe e
                    LEFT JOIN profil pro ON e.id_profil = pro.id_profil
                    LEFT JOIN civilite civ ON e.id_civilite = civ.id_civilite
                    WHERE
                        e.id_employe = '.intval($id);
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $profil = new Profil($p['proid_profil'], $p['prolibelle'], $p['provalide']);
                $civilite = new Civilite($p['civid_civilite'], $p['civlibelle'], $p['civdiminutif'], $p['civvalide']);
                return new Employe($p['eid_employe'], $civilite, $profil, $p['emdp'], $p['enom'], $p['eprenom'], 
                                    $p['etelephone'], $p['emobile'], $p['eemail'], $p['edate_naissance'], $p['edate_ajout'], 
                                    $p['edate_modif'], $p['evalide']);
            }

            return null;
        }
		
    }
?>